/**
 * @desc
 *
 * @使用场景
 *
 * @author  杨晓东[of1081]
 * @company qianmi.com
 * @Date    16/8/29
 **/
'use strict';



let pcHeadCommon = '.x-head-1 {display: block;}.x-head-2 {display: block;}.x-head-3 {display: block;}';
let pcFootCommon = '.x-foot-1 {display: block;}.x-foot-2 {display: block;}.x-foot-3 {display: block;}';


export default class displayRuleUtil{


  static constDefaultPcHeadCommon =pcHeadCommon;

  static constDefaultPcFootCommon =pcFootCommon;


  /**
   *
   * @param ruleList  [{url:"/list.html",headLv:3,footLv:3}]
   * @return {Array}
   */
  static generateCommonDisplayRule = function (ruleList) {
    return ruleList.map((item, _index)=> {
      return _generateRultItem({url: item.url,
        headLv: item.headLv,
        footLv: item.footLv,
        type:item.type,
        operate:item.operate
      });
    });
  }

  /**
   * 获取匹配的规则
   * @param url
   * @param ruleArray
   * @return {Array}
   */
  static getHitRules = function (url,ruleArray){

    let hitRules = [];
    for (let i = 0, ilen = ruleArray.length; i < ilen; i++) {
      let _ruleInfo = ruleArray[i];
      if (_ruleInfo.type === 'string') {
        let _index = url.indexOf(_ruleInfo.url);
        if (_ruleInfo.operate === 'beginWith' && _index === 0) {
          hitRules.push(_ruleInfo);
        } else if (_ruleInfo.operate === 'equal' && _ruleInfo.url ===url) {
          hitRules.push(_ruleInfo);
        } else if (_ruleInfo.operate === 'endWith' && (_index + _ruleInfo.url.length) === url.length) {
          hitRules.push(_ruleInfo);
        } else if (_ruleInfo.operate === 'include' && _index >= 0) {
          hitRules.push(_ruleInfo);
        }
      }
    }

    return hitRules;
  }


  /**
   * 获取第一个匹配的规则
   * @param url
   * @param ruleArray
   * @return {*}
   */
  static getFirstHitRule = function (url,ruleArray){
    let relues =  displayRuleUtil.getHitRules(url,ruleArray);
    if(relues && relues.length>0){
      return relues[0]
    }else{
      return null;
    }
  }



  /**
   *
   * @param ruleInfo  {headLv:3,footLv:3}
   * @return {{}}
   */
  static getRuleCssContent = function (ruleInfo){

    if(!ruleInfo){
      throw new Error('传入值ruleInfo为空');
    }

    let result ={};

    if(ruleInfo.headLv >= 0){
      let headCss = pcHeadCommon;
      for (let i = 1, ilen = 3; i <= ilen; i++) {
        if (i > ruleInfo.headLv) {
          console.log('设置x-head-' + i + "none");
          headCss = headCss.replace('.x-head-' + i + ' {display: block;}', '.x-head-' + i + ' {display: none;}')
        }
      }
      result['headCss'] = headCss;
    }

    if(ruleInfo.footLv >= 0){
      let footCss = pcFootCommon;
      for (let i = 1, ilen = 3; i < ilen; i++) {
        if (i > ruleInfo.footLv) {
          footCss = footCss.replace('.x-foot-' + i + ' {display: block;}', '.x-foot-' + i + ' {display: none;}')
        }
      }
      result['footCss'] = footCss;
    }

    return result;
  }
}




/**
 *
 * @param url       页面对应URL
 * @param headLv    公共头部要显示的级别 ,小于等于该级别的都会显示出来,有效值1-3
 * @param footLv    公共尾部要显示的级别 ,小于等于该级别的都会显示出来
 * @param type      类型
 * @param operate   对比方式 string :  beginWith include endWith
 *                          regular: 是否命中
 * @private
 */
function _generateRultItem({url,headLv,footLv,type="string",operate="beginWith"}){

  if(typeof(headLv) != 'number' || headLv < 0  ||  headLv >3){
    throw new Error(url+'对应的headLv 级别不符合要求,正常值应该是数字,1-3:'+headLv);
  }

  if(typeof(footLv) != 'number' || footLv < 0  ||  footLv >3){
    throw new Error(url+'对应的footLv 级别不符合要求,正常值应该是数字,1-3:'+footLv);
  }

  return {
    url:url,//包含什么则,显示
    headLv,
    footLv,
    type, //type:"regular", //正则表达式,默认是string
    operate
  }
}
